<?php
namespace app\member\controller;
use app\ApiRest;
use app\member\info\PermissionMember;
use app\member\model\CashRecord;
use app\member\model\Goods;
use app\member\model\Growth;
use app\member\model\Level;
use app\member\model\Member;
use app\member\model\Rights;
use app\overlord\info\PermissionOverlord;
use app\shop\model\ShareCouponId;
use app\member\model\StoredOrder;
use app\member\model\Config;
use app\shop\model\ShareCoupon;
use app\shop\controller\IndexWxPay;
use app\shop\model\IndexCoupon;
use think\App;
use app\member\model\Stored as model;
use think\facade\Db;


class IndexStored extends ApiRest
{

    protected $model;

    protected $config_model;

    protected $level_model;

    protected $rights_model;

    protected $order_model;

    protected $share_coupon_model;


    public function __construct(App $app) {

        parent::__construct($app);

        $this->model = new model();

        $this->config_model = new Config();

        $this->level_model = new Level();

        $this->rights_model = new Rights();

        $this->order_model  = new StoredOrder();

        $this->member_model  = new Member();

        $this->share_coupon_model  = new ShareCoupon();

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-10 17:44
     * @功能说明:储值列表
     */
    public function storedList(){

        $dis = [

            'uniacid' => $this->_uniacid,

            'status'  => 1
        ];

        $data = $this->model->storedList($dis,10);

        $member_model = new Member();

        $data['user_info'] = $member_model->userInfo($this->getUserId(),$this->_uniacid);

        $overlord = new PermissionOverlord($this->_uniacid);

        $data['user_info']['overlord_auth'] = $overlord->pAuth();

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-14 17:46
     * @功能说明:储值详情
     */
    public function storedInfo(){

        $input = $this->_param;

        $dis = [


            'id' => $input['id']
        ];

        $data = $this->model->storedInfo($dis);

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-10 17:50
     * @功能说明:
     */
    public function payOrder(){

        $input = $this->_input;
        //分享人
        $share_id = !empty($input['share_id'])?$input['share_id']:0;

        $dis = [

            'id' => $input['id']

        ];

        $data  = $this->model->storedInfo($dis);

        if(empty($data)){

            $this->errorMsg('充值卡已下架');
        }
        //下单
        $order = $this->order_model->addOrder($data,$this->getUserId(),$input['staff_id'],$share_id);

        $pay_controller = new IndexWxPay($this->app);

        $user           = $this->getUserInfo();
        //支付
        $jsApiParameters= $pay_controller->createWeixinPay($this->payConfig(),$user['openid'],$this->_uniacid,"储值充值",['type' => 'storedPay' , 'out_trade_no' => $order['order_code']],$order['pay_price']);

        return $this->success($jsApiParameters);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-15 16:23
     * @功能说明:
     */
    public function storedMemberInfo(){

        $input = $this->_input;

        $dis= [

            'b.id' => $input['id']
        ];

        $data  = $this->member_model->memberInfoIndex($dis);
        //说明是plus会员
        if($data['over_time']>time()){

            $data['member_title'] = $this->level_model->where(['top'=>999,'uniacid'=>$this->_uniacid])->value('title');

        }else{
            //普通会员
            $level = $this->level_model->getUserLevel($input['id'],$this->_uniacid);

            $data['member_title'] = !empty($level)?$level['title']:'';
        }
        //电话
        $data['member_phone'] = Db::name('longbing_card_user_phone')->where(['user_id'=>$input['id']])->value('phone');

        return $this->success($data);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-16 11:09
     * @功能说明:员工扣款
     */
    public function staffDeduction(){

        $input = $this->_input;

        if($this->getUserInfo()['is_staff']!=1){

            $this->errorMsg('只有员工才能扣款哦');
        }

        if(!isset($input['time'])||$input['time']+600<time()){

            $this->errorMsg('支付码已过期，请重新生成');

        }
        //查看会员信息
        $member_info = $this->member_model->memberUpdateInfo(['user_id'=>$input['id']]);
        //判断余额
        if($input['price']>$member_info['stored']){

            $this->errorMsg('储值不足');
        }

        $res = $this->order_model->desStore($input['price'],$input['content'],$member_info,2,$this->getUserId());

        return $this->success($res);

    }


    /**
     * @author chenniang
     * @DataTime: 2020-09-16 15:31
     * @功能说明:储值记录
     */
    public function orderList(){

        $input = $this->_input;

        $dis[] = ['uniacid','=',$this->_uniacid];

        $dis[] = ['user_id','=',$this->getUserId()];

        $dis[] = ['status','=',2];

        if(!empty($input['title'])){

            $dis[] = ['title','like','%'.$input['title'].'%'];

        }

        $data = $this->order_model->recordList($dis,$input['limit']);

        return $this->success($data);


    }


    /**
     * @author chenniang
     * @DataTime: 2020-11-30 18:32
     * @功能说明:申请提现
     */
    public function applyTx(){

        $input = $this->_input;
        //查看会员信息
        $member_info = $this->member_model->memberUpdateInfo(['user_id'=>$this->getUserId()]);
        //判断余额
        if($input['price']>$member_info['stored']||$input['price']>$member_info['cash_stored']){

            $this->errorMsg('储值不足');
        }

        $insert = [

            'uniacid'     => $this->_uniacid,

            'user_id'     => $this->getUserId(),

            'apply_price' => $input['price'],

            'true_price'  => $input['price'],

            'create_time' => time(),

            'status'      => 1,

            'wx_code'     => $input['wx_code']
        ];

        $cash_model = new CashRecord();

        Db::startTrans();

        $res = $cash_model->insert($insert);

        if($res!=1){

            $this->errorMsg('申请失败');
        }

        $record_id = $cash_model->getLastInsID();
        //添加提现记录并且减掉余额
        $res = $this->order_model->desStore($insert['apply_price'],'',$member_info,7,$this->getUserId(),'',$record_id);

        if($res!=1){

            $this->errorMsg('申请失败');
        }

        Db::commit();

        return $this->success(1);
    }


    /**
     * @author chenniang
     * @DataTime: 2020-12-01 11:23
     * @功能说明:用户提现记录
     */
    public function userCashList()
    {
        $input = $this->_input;

        $cash_model = new CashRecord();

        $dis[] = ['a.uniacid','=',$this->_uniacid];

        $dis[] = ['a.user_id','=',$this->getUserId()];

        if(!empty($input['name'])){

            $dis[] = ['b.nickName','like','%'.$input['name'].'%'];
        }

        $data = $cash_model->dataList($dis,$input['limit']);

        return $this->success($data);


    }










}
